package com.cy.ppj.service.withdraw.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cy.ppj.dao.mapper.user.UserInfoMapper;
import com.cy.ppj.model.domian.user.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.ppj.dao.mapper.account.WithdrawMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.withdraw.WithdrawApplyAO;
import com.cy.ppj.model.domian.account.Withdraw;
import com.cy.ppj.model.dto.account.AccountAmountDTO;
import com.cy.ppj.model.enums.DealType;
import com.cy.ppj.model.enums.WithdrawStatus;
import com.cy.ppj.model.query.WithdrawAdminQuery;
import com.cy.ppj.model.query.WithdrawQuery;
import com.cy.ppj.model.vo.WithdrawAdminVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.account.AccountService;
import com.cy.ppj.service.withdraw.WithdrawService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * 提现服务实现类
 * @author: tanyilin
 * @create: 2018年7月31日 下午2:02:25
 */
@Slf4j
@Service
public class WithdrawServiceImpl extends BaseService implements WithdrawService{
	
	@Autowired
	private AccountService accountService;
	@Autowired
	private WithdrawMapper withdrawMapper;
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Override
	@Transactional
	public void withdrawApply(WithdrawApplyAO withdrawApplyAO, Long userId) {
		Date now = new Date();
		BigDecimal amount = withdrawApplyAO.getAmount();

		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo = userInfoMapper.selectOne(userInfo);
		state(userInfo != null, "用户未实名");
		state(StringUtils.hasLength(userInfo.getBankCard()), "用户未绑卡");

		//提现记录
		Withdraw withdraw = new Withdraw();
		withdraw.setUserId(userId);
		withdraw.setAmount(amount);
		withdraw.setFeeAmount(withdrawApplyAO.getFeeAmount());
		withdraw.setBankCard(userInfo.getBankCard());
		withdraw.setBankNo(userInfo.getBankNo());
		withdraw.setStatus(WithdrawStatus.APPLY);
		withdraw.setCreateTime(now);
		withdrawMapper.insertSelective(withdraw);
		//保存资金交易记录
		AccountAmountDTO accountAmountDTO = new AccountAmountDTO();
		accountAmountDTO.setAmount(amount);
		accountAmountDTO.setUserId(userId);
		accountAmountDTO.setType(DealType.WITHDRAW_APPLY);
		accountAmountDTO.setUpdateTime(now);
		accountService.transaction(accountAmountDTO);
	}

	@Override
	@Transactional
	public void withdrawReturn(Long withdrawId) {
		Date now = new Date();
		//先找到需要进行‘提现拒绝’操作的提现记录
		Withdraw withdraw = new Withdraw();
		withdraw.setId(withdrawId);
		withdraw = withdrawMapper.selectOne(withdraw);
		state(withdraw != null, "该提现记录不存在");
		//更新提现记录状态为‘提现退回’
		withdraw.setStatus(WithdrawStatus.RETURN);
		withdraw.setUpdateTime(now);
		int result = withdrawMapper.updateByPrimaryKeySelective(withdraw);
		state(result == 1, "更新提现记录状态失败");
		//保持资金交易记录
		AccountAmountDTO accountAmountDTO = new AccountAmountDTO();
		accountAmountDTO.setUserId(withdraw.getUserId());
		accountAmountDTO.setAmount(withdraw.getAmount());
		accountAmountDTO.setType(DealType.WITHDRAW_RETURN);
		accountAmountDTO.setUpdateTime(now);
		accountService.transaction(accountAmountDTO);
	}

	@Override
	@Transactional
	public void withdrawSuccess(Long withdrawId) {
		Date now = new Date();
		//先找到需要进行‘提现成功’操作的提现记录
		Withdraw withdraw = new Withdraw();
		withdraw.setId(withdrawId);
		withdraw = withdrawMapper.selectOne(withdraw);
		state(withdraw != null, "该提现记录不存在");
		//更新提现记录状态为‘提现成功’
		withdraw.setStatus(WithdrawStatus.SUCCESS);
		withdraw.setUpdateTime(now);
		int result = withdrawMapper.updateByPrimaryKeySelective(withdraw);
		state(result == 1, "更新提现记录状态失败");
		//保持资金交易记录
		AccountAmountDTO accountAmountDTO = new AccountAmountDTO();
		accountAmountDTO.setUserId(withdraw.getUserId());
		accountAmountDTO.setAmount(withdraw.getAmount());
		accountAmountDTO.setType(DealType.WITHDRAW_SUCCESS);
		accountAmountDTO.setUpdateTime(now);
		accountService.transaction(accountAmountDTO);
	}

	@Override
	public PageBean<Withdraw> queryWithdtawList(WithdrawQuery withdrawQuery) {
		int total = withdrawMapper.queryWithdrawListCount(withdrawQuery);
		List<Withdraw> list = new ArrayList<>();
		if(total > 0) {
			withdrawQuery.startPage();
			list = withdrawMapper.queryWithdrawList(withdrawQuery);
		}
		return new PageBean<>(list, total);
	}

	@Override
	public PageBean<WithdrawAdminVO> queryWithdrawListAdmin(WithdrawAdminQuery withdrawAdminQuery) {
		int total = withdrawMapper.queryWithdrawAdminListCount(withdrawAdminQuery);
		List<WithdrawAdminVO> list = new ArrayList<>();
		if(total > 0) {
			withdrawAdminQuery.startPage();
			list = withdrawMapper.queryWithdrawAdminList(withdrawAdminQuery);
			list.stream().forEach(w -> {
				w.setStatusText(w.getStatus().getRemark());
			});
		}
		return new PageBean<>(list, total);
	}

	@Override
	public Result selectWithdrawStatistics() {
		return Result.success("查询成功", withdrawMapper.selectWithdrawStatistics());
	}

}
